﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using Asko.Lib.Algorithms.Layout;
using Asko.Lib.Controls.Animations;
using GalaSoft.MvvmLight.Messaging;
using Asko.Lib.Messages;
using System.Collections.Generic;
using System.Windows.Media;

namespace Asko.Lib.Views
{
    /// <summary>
    /// Interaction logic for GraphView.xaml
    /// </summary>
    public partial class GraphView : UserControl
    {
        #region Champs
        private ILayoutAlgorithm _algorithme;
        #endregion

        #region Méthodes
        public GraphView()
        {
            _algorithme = null;
            InitializeComponent();
            Messenger.Default.Register<LayoutMessage>(this, x => this.ExecuteLayout(x));

        }
        /// <summary>
        /// Exécute 
        /// </summary>
        /// <param name="message"></param>
        private void ExecuteLayout(LayoutMessage message)
        {
            if (message == LayoutMessage.Update)
            {
                this.MoveAnimation();
            }
        }
        private void MouseDragElementBehavior_DragFinished(object sender, MouseEventArgs e)
        {
            NodeView nvNoeud;
            nvNoeud = e.Source as NodeView;
            nvNoeud.X = nvNoeud.RenderTransform.Value.OffsetX;
            nvNoeud.Y = nvNoeud.RenderTransform.Value.OffsetY;
        }

        public void MoveAnimation()
        {
            TimeSpan tstemps;
            DateTime dtDebut, dtFin, dtFin2;
            SimpleAnimation saAnimation;

            _algorithme = new CircularLayoutAlgorithm();
            _algorithme.View = this;
            dtDebut = DateTime.Now;
            _algorithme.Compute();
            dtFin = DateTime.Now;
            tstemps = dtFin - dtDebut;
            Console.WriteLine(tstemps);
            for (int i = 0; i < _algorithme.Count; i++)
            {
                saAnimation = new SimpleAnimation();
                saAnimation.Position = _algorithme.Positions[i];
                saAnimation.Node = _algorithme.Nodes[i];
                saAnimation.Animate();
            }
            dtFin2 = DateTime.Now;
            tstemps = dtFin2 - dtDebut;
            Console.WriteLine(tstemps);        

        }

        private void NodeView_Loaded(object sender, RoutedEventArgs e)
        {
            Messenger.Default.Send<LayoutMessage>(LayoutMessage.Update);
        }
        #endregion

        #region Propriétés
        /// <summary>
        /// Retourne les noeuds
        /// </summary>
        public IEnumerable Nodes
        {
            get { return icNodes.ItemsSource; }
        }

        #endregion


    }
}
